<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/sys/dict/";  //应用的地址名
String addActionName = path+"/sys/dict/SysDictM.action";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
<%@ include file="/common/common.jsp"%>
<script type="text/javascript" src="<%=path%>/js/My97/WdatePicker.js"></script>

    
    <title>统计报表起止日期设置</title>
<script type="text/javascript">
<!--
           $(function(){
               <%if("true".equals(request.getParameter("isSubmit"))){%>
               $.messager.alert("消息提示","设置信息已经保存！","info");
               <%}%>
               
			   $('#add').click(function(){
	                  $('form').submit();
		            });
               //初始化工作，因为远程载入该界面时无法通过在标签中进行设置相应的验证属性等信息，必须要在这里进行初始化
               //$('#add').linkbutton();
               var begin = '<s:property value="#request.begin"/>';
               var end = '<s:property value="#request.end"/>';
               var type = 0;
               if(begin.indexOf("#")>0){
                   type = begin.split("#")[0];begin=begin.split("#")[1];end=end.split("#")[1];
                   $('input[name="type"][value="'+type+'"]').attr('checked',true);
                   initHtml(type);
            	   $('#begin').val(begin.split('|')[0]);
            	   $('#end').val(end.split('|')[0]);
            	   $('#dayBegin').val(begin.split('|')[1]);
            	   $('#dayEnd').val(end.split('|')[1]);
                   showDemo();
               }
               else{
                   initHtml(0);
               }
	            
           });
     function getVal(name){
    	 var val = '';
    	 $('input[name="'+name+'"]').each(function(){
    		 if(this.checked)val=this.value;
    	 });
    	 return val;
     }
     function toStr(d){
    	 if(!d)return '';
    	 str= d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate();
    	 str = str.replace(/\-(\d)\-/,'-0$1-').replace(/\-(\d)$/,'-0$1');
    	 return str;
     }
     function getLastDay(d){
    	 if(!d)return '';
    	 var dd = new Date(d.getTime());
    	 dd.setMonth(dd.getMonth()+1);
    	 dd.setUTCDate(0);
    	 return dd.getUTCDate();
     }
     function showDemo(){
		var type = getVal('type');
		var d = new Date();
		var str = '今天是'+toStr(d)+'，';
		var b = $('#begin').val(),e = $('#end').val(),bd = $('#dayBegin').val(),ed = $('#dayEnd').val();
		switch (type) {
		case '0'://按天
			d.setDate(d.getDate()-(bd));str+=('开始日期为'+toStr(d)+'，');
			d.setDate(new Date().getDate()-(ed));str+=('结束日期为'+toStr(d)+'');
			break;
		case '1'://按周js中与java中的每周第几天表示方法不一样，JS以0开始，6结束，java中以1开始，7结束表示从周日到周六的日期
			var bdiff = b*7+d.getDay()-bd+1,ediff = e*7+d.getDay()-ed+1;
			d.setTime(d.getTime()-(bdiff*24*3600*1000));str+=('开始日期为'+toStr(d)+'，');
			d.setTime(new Date().getTime()-(ediff*24*3600*1000));str+=('结束日期为'+toStr(d)+'');
			break;
		case '2'://按月
			var maxDay =0;
			d.setMonth(d.getMonth()-b);maxDay=getLastDay(d);d.setDate(Math.min(maxDay,bd));str+=('开始日期为'+toStr(d)+'，');
			d=new Date();d.setMonth(d.getMonth()-e);maxDay=getLastDay(d);d.setDate(Math.min(maxDay,ed));str+=('结束日期为'+toStr(d)+'');
			break;
		case '3'://按季度

			break;
		case '4'://按年
			str+=('开始日期为'+(d.getFullYear()-b)+'-'+bd+'，');
			str+=('结束日期为'+(d.getFullYear()-e)+'-'+ed+'');
			break;
		default:
			d.setTime(d.getTime()-(bd*24*3600*1000));str+=('开始日期为'+toStr(d)+'，');
			d.setTime(new Date().getTime()-(ed*24*3600*1000));str+=('结束日期为'+toStr(d)+'');
			break;
		}
		$('#beginInput').val(type+"#"+b+'|'+bd);
		$('#endInput').val(type+"#"+e+"|"+ed);
		$('#demo').html(str.replace(/(\d{4}-\d{2}-\d{2})/g,'<font color="#FFA500">$1</font>'));
     }

	function initHtml(type) {
		if (type == null)
			type = getVal('type');
		var btd = '', etd = '', js = '',comment = '';
		switch (type) {
		case '1'://按周
			var opt = '', ary = '日,一,二,三,四,五,六'.split(',');
			for ( var i = 0; i < 7; i++)
				opt += '<option value=' + (i + 1) + '>周' + ary[i] + '</option>';
			btd = '前 <input type=text id="begin"> 周 <select id="dayBegin">' + opt + '</select> ';
			etd = '前 <input type=text id="end"> 周 <select id="dayEnd">' + opt + '</select> ';
			comment = '<font color=red>注：后一周为-1,当前周为0，前一周为1，前两周为2，依次类推。每周计算顺序为从周日到周六</font>';
			js = "$('#begin').val(1);$('#end').val(0);$('#dayBegin').val('7');$('#dayEnd').val('6');";
			break;
		case '2'://按月
			var opt = '';
			for ( var i = 0; i < 31; i++)
				opt += '<option value=' + (i<9?'0':'')+(i + 1) + '>' + (i+1) + '日</option>';
			btd = '前 <input type=text id="begin"> 月 <select id="dayBegin">' + opt + '</select> ';
			etd = '前 <input type=text id="end"> 月 <select id="dayEnd">' + opt + '</select> ';
			comment = '<font color=red>注：后一月为-1,当前月为0，前一月为1，前两月为2，依次类推。选择31日时即为指定月份最后一天，如本月只有29天则最终结果为29日</font>';
			js = "$('#begin').val(1);$('#end').val(0);$('#dayBegin').val('26');$('#dayEnd').val('25');";
			break;
		case '3'://按季度

			break;
		case '4'://按年
			var opt = '';
			btd = '前 <input type=text id="begin"> 年 <input type=text readonly=true id="dayBegin" class="Wdate" onclick="WdatePicker({dateFmt:\'MM-dd\'})" style="width:60px;"> ';
			etd = '前 <input type=text id="end"> 年 <input type=text readonly=true id="dayEnd" class="Wdate" onclick="WdatePicker({dateFmt:\'MM-dd\'})" style="width:60px;"> ';
			comment = '<font color=red>注：后一年为-1,当前年为0，前一年为1，前两年为2，依次类推...</font>';
			js = "$('#begin').val(0);$('#end').val(0);$('#dayBegin').val('01-01');$('#dayEnd').val('12-31');";
			break;
		default:
			btd = '前 <input type=text id="dayBegin"> 天';
			etd = '前 <input type=text id="dayEnd"> 天 ';
			comment = '<font color=red>注：明天为-1,当天为0，昨天为1，前天为2，依次类推...</font>';
			js = "$('#dayBegin').val(6);$('#dayEnd').val(0);";
			break;
		}
		$('input[name="type"][value="'+type+'"]').attr('checked', true);
		$('#beginTd').html(btd);
		$('#endTd').html(etd);
		$('#comment').html(comment);
		$('#begin,#end,#dayBegin,#dayEnd').keyup(showDemo);
		$('#begin,#end,#dayBegin,#dayEnd').keypress(showDemo);
		$('#begin,#end,#dayBegin,#dayEnd').change(showDemo);
		eval(js);
		showDemo();
	}
//-->
</script>
<style type="text/css">
.tb_add_th td input[type=text]{width:40px;}
.tb_add_th td select{width:60px;}
.tb_add_th td input[type=text]:hover{width:40px;}
</style>
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
<s:form method="post">
    <s:hidden id="op"  name="op"></s:hidden>
    <input type="hidden" name="isSubmit" value="true"/>
    <input type=hidden name="dictName"  value='<s:property value="#request.dictName"/>'>
    <input type=hidden id="beginInput" name="begin"  value='<s:property value="#request.begin"/>'>
    <input type=hidden id="endInput" name="end"  value='<s:property value="#request.end"/>'>
	<table align="center" width="100%"  class="tb_add_th">
<tr><th width="80">类型</th><td colspan="2"><s:radio onclick="initHtml()" name="type" list="#{'0':'按天','1':'按周','2':'按月','4':'按年'}"></s:radio></td></tr>
<tr><th>开始日期</th><td id="beginTd" nowrap></td><td rowspan="2" id="comment"></td></tr>
<tr><th>结束日期</th><td id="endTd" nowrap></td></tr>
<tr><th>示例</th><td colspan="2" id="demo"></td></tr>
<tr><th colspan="3"><a id="add"  iconCls="icon-save" href="javascript:void(0)"  class="easyui-linkbutton" >保存</a></th></tr>
	</table>    
</s:form>
</body>
</html>


